﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace carApp
{
    public partial class Goods : Form
    {
        public Goods()
        {
            InitializeComponent();
        }

        private void Goods_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            LoadData();
            if(carApp.AppConfig.typeid=="0")
            {
                button2.Visible = false;
                dataGridView1.Columns[6].Visible = false;
            }
        }
        #region pager
        int pageSize = 20;
        int CurrentPage = 1;
        int pageCount = 0;
        DataTable dt;
        int CurrentRowIndex = 0;
        int CurrentColumnIndex = 0;
        void LoadData()
        {
            string where = "";
            if (txtKeyword.TextLength > 0)
                where = " name like'%" + txtKeyword.Text + "%'";
            int RowCount = db.count("goods",where);
            dt = db.GetPagerData("goods", "*", where, pageSize, CurrentPage);
            pageCount = (int)Math.Ceiling(RowCount * 1.0 / pageSize);
            lblPageCount.Text = "共 " + pageCount + " 页";
            txtPageCurrent.Text = CurrentPage.ToString();
            dataGridView1.DataSource = dt;
        }
        private void lblfirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 1;
            LoadData();
        }

        private void lblprev_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            if (CurrentPage < 1)
                CurrentPage = 1;
            LoadData();
        }

        private void lblNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            if (CurrentPage > pageCount)
                CurrentPage = pageCount;
            LoadData();
        }

        private void lbllast_Click(object sender, EventArgs e)
        {
            CurrentPage = pageCount;
            LoadData();
        }
        private void btnPageGo_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse(txtPageCurrent.Text);
            LoadData();
        }
        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value;
            Model.Goods mod = new Model.Goods();
            mod = db.FillModel<Model.Goods>("goods", "id=" + id);
            if (mod != null)
            {
                txtSN.Text = mod.sn;
                txtBarcode.Text = mod.color;
                txtName.Text = mod.name;
                txtnum.Text = mod.stock.ToString();
                txtSpec.Text = mod.spec;
                supply_cmb.Text = mod.supplyer;
                txtPack.Text = mod.pack;
                txtPackNum.Text = mod.packnum.ToString();
                txtUnit.Text = mod.unit;
                txtPrice.Text = Math.Round(mod.price,2).ToString();
                txtPrice1.Text = Math.Round(mod.price1, 2).ToString();
                txtPrice2.Text = Math.Round(mod.price2, 2).ToString();
                lblId.Text = mod.id.ToString();
                panel_add.Visible = true;
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value;
            Libs.OleDbHelper.ExecuteSql("delete from goods where id=" + id);
            this.dataGridView1.Rows.RemoveAt(CurrentRowIndex);
        }
        #endregion
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            lblId.Text = "0";
            panel_add.Left = this.Width / 2 - 290;
            panel_add.Top = this.Height / 2 - 220;
            txtSN.Text = "G"+DateTime.Now.ToString("yyyyMMddhhmmss");
            panel_add.Visible = true;
            DataTable dt = db.GetTable("danwei");
            supply_cmb.DisplayMember = "name";
            supply_cmb.DataSource = dt;
        }

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            var dgv = (DataGridView)sender;
            CurrentRowIndex = e.RowIndex;
            CurrentColumnIndex = e.ColumnIndex;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && CurrentRowIndex>-1)
            {
                CurrentRowIndex = e.RowIndex;
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
                contextMenuStrip1.Show(Cursor.Position);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            string where = "";
            if (txtKeyword.TextLength > 0)
                where = " name like'%" + txtKeyword.Text + "%'";
            dt = db.GetTable("goods", where, "sn,type,name,spec,unit,color,stock,out_stock,supplyer");
            if(dt.Rows.Count==0)
            {
                MessageBox.Show("没有数据");
                return;
            }
            var csvPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            if(csvPath=="")
            {
                csvPath = Application.StartupPath.TrimEnd();
            }
            var csvName = csvPath + "\\产品" +DateTime.Now.ToString("yyyyMMddHHmmss")+".csv";
            
            if (Libs.FileHelper.DataToCSV(dt, csvName, "编号,类型,产品名称,规格,单位,颜色,库存,出库量,供应商"))
            {
                MessageBox.Show("导出成功");
                System.Diagnostics.Process.Start("explorer.exe", csvPath + @"\");
            }
            else
            {
                MessageBox.Show("导出失败");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel_add.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnAddSave_Click(object sender, EventArgs e)
        {
            if (txtName.TextLength == 0)
            {
                MessageBox.Show("请填写商品名称");
                return;
            }
            int num = 0;
            if (lblId.Text == "0")
            {
                if (txtnum.TextLength > 0)
                {
                    num = int.Parse(txtnum.Text);
                }
            }

            panel_add.Visible = false;
            try
            {
                if (lblId.Text == "0")
                {
                    if (db.count("goods", "sn='" + txtSN.Text + "'") > 0)
                    {
                        //db.inc("goods", "sn='" + txtSN.Text + "'", "stock", int.Parse(txtnum.Text));
                        //db.inc("goods", "sn='" + txtSN.Text + "'", "in_stock", int.Parse(txtnum.Text));
                    }
                    else
                    {
                        Model.Goods goods = new Model.Goods();
                        goods.color = txtBarcode.Text;
                        goods.name = txtName.Text;
                        goods.sn = txtSN.Text;
                        goods.sort = 0;
                        goods.spec = txtSpec.Text;
                        goods.buy_price = decimal.Parse(txtbuyPrice.Text);
                        goods.price = decimal.Parse(txtPrice.Text);
                        goods.price1 = decimal.Parse(txtPrice1.Text);
                        goods.price2 = decimal.Parse(txtPrice2.Text);
                        goods.price3 = 0;
                        goods.stock = int.Parse(txtnum.Text);
                        goods.supplyer = supply_cmb.Text;
                        goods.pack = txtPack.Text;
                        goods.packnum = int.Parse(txtPackNum.Text);
                        goods.unit = txtUnit.Text;
                        goods.in_stock = int.Parse(txtnum.Text);
                        if (db.add(goods) > 0)
                        {
                            MessageBox.Show("入库成功");
                        }
                        else
                        {
                            MessageBox.Show("入库失败");
                        }
                    }
                    Model.Store store = new Model.Store();
                    store.color = txtBarcode.Text;
                    store.name = txtName.Text;
                    store.sn = txtSN.Text;
                    store.username = txtUsername.Text;
                    store.spec = txtSpec.Text;
                    store.num = int.Parse(txtnum.Text);
                    store.supplyer = supply_cmb.Text;
                    store.type = txtPack.Text;
                    store.unit = txtUnit.Text;
                    store.buy_price = decimal.Parse(txtbuyPrice.Text);
                    store.sell_price = decimal.Parse(txtPrice.Text);
                    store.times = txtPackNum.Text == "" ? DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") : txtPackNum.Text;
                    db.add(store);
                }
                else
                {
                    Model.Goods goods = new Model.Goods();
                    goods = db.FillModel<Model.Goods>("goods", "id=" + lblId.Text);
                    goods.id = int.Parse(lblId.Text);
                    goods.color = txtBarcode.Text;
                    goods.name = txtName.Text;
                    goods.sn = txtSN.Text;
                    goods.sort = 0;
                    goods.spec = txtSpec.Text;
                    if(carApp.AppConfig.typeid=="1")
                        goods.buy_price = decimal.Parse(txtbuyPrice.Text);
                    goods.price = decimal.Parse(txtPrice.Text);
                    goods.price1 = decimal.Parse(txtPrice1.Text);
                    goods.price2 = decimal.Parse(txtPrice2.Text);
                    //goods.stock = int.Parse(txtnum.Text);
                    goods.supplyer = supply_cmb.Text;
                    goods.pack = txtPack.Text;
                    goods.packnum = int.Parse(txtPackNum.Text);
                    goods.unit = txtUnit.Text;
                    if (db.edit(goods) > 0)
                    {
                        MessageBox.Show("修改成功");
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }
                LoadData();
            }
            catch (Exception ex)
            {
                ReportEx.upPortEX(ex.Message);
            }
        }

        private void 入库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value;
            Model.Goods mod = new Model.Goods();
            mod = db.FillModel<Model.Goods>("goods", "id=" + id);
            if (mod != null)
            {
                string str = Microsoft.VisualBasic.Interaction.InputBox("请输入入库数量", "商品入库", "0", -1, -1);
                if (str.Length>0)
                {
                    try
                    {
                        db.inc("goods","id="+id,"stock",int.Parse(str));
                        LoadData();
                        MessageBox.Show("入库成功");
                    }
                    catch
                    {
                        MessageBox.Show("添加失败");
                    }
                }
            }
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            db.remove("goods");
            LoadData();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 11)
            {
                if (e.RowIndex > -1)
                {
                    DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                    if (row != null)
                    {
                       
                        int kucun = int.Parse(row.Cells[8].Value.ToString());
                        int packnum = int.Parse(row.Cells[10].Value.ToString());
                        if (packnum > 0)
                        {
                            string pack = row.Cells[9].Value.ToString();
                            int zhengshu = (int)(kucun / packnum);
                            int shang = kucun % packnum;
                            string txt = zhengshu.ToString() + pack;
                            if (shang > 0)
                                txt += shang.ToString() + row.Cells[3].Value.ToString();
                            e.FormattingApplied = true;
                            e.Value = string.Format("{0}", txt);
                        }
                    }
                }
            }
        }
    }
}
